package com.ovopark.organize.server.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ovopark.organize.server.mapper.FranchiseesRegistrationFollowMapper;
import com.ovopark.organize.server.model.entity.FranchiseesRegistration;
import com.ovopark.organize.server.model.entity.FranchiseesRegistrationFollow;
import com.ovopark.organize.server.model.entity.FranchiseesRegistrationPlan;
import com.ovopark.organize.server.model.vo.FranchiseesRegistrationFollowVo;
import com.ovopark.organize.server.model.vo.SimpleDateVo;
import com.ovopark.organize.server.service.FranchiseesRegistrationFollowService;
import com.ovopark.organize.server.service.FranchiseesRegistrationPlanService;
import com.ovopark.organize.server.service.FranchiseesRegistrationService;
import com.ovopark.organize.server.util.CollectionUtil;
import com.ovopark.organize.server.util.CommonUtil;
import com.xxl.job.core.util.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jetty.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.ParseException;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class FranchiseesRegistrationFollowServiceImpl extends ServiceImpl<FranchiseesRegistrationFollowMapper, FranchiseesRegistrationFollow> implements FranchiseesRegistrationFollowService {

    Logger logger = LoggerFactory.getLogger(FranchiseesRegistrationFollowServiceImpl.class);

    @Resource
    private FranchiseesRegistrationService franchiseesRegistrationService;
    @Resource
    private FranchiseesRegistrationPlanService franchiseesRegistrationPlanService;

    @Override
    public FranchiseesRegistrationFollow saveOrUpdateVo(FranchiseesRegistrationFollow follow) {
        if (follow.getId() != null) {
            baseMapper.updateById(follow);
        } else {
            baseMapper.insert(follow);
        }
        return follow;
    }

    @Override
    public FranchiseesRegistrationFollow getFollowById(Integer id) {
        return baseMapper.selectById(id);
    }

    @Override
    public void deleteFollowById(Integer id) {
        // 删除 跟进记录 如果 当前跟进记录是 最新的一个， 需要把 线索的 跟进人 换成 后面一个；
        FranchiseesRegistrationFollow follow = getFollowById(id);
        Integer followUpPlan = follow.getFollowUpPlan();
        FranchiseesRegistration franchiseesRegistration = franchiseesRegistrationService.findFranchiseesRegistrationById(follow.getFranchiseesRegistrationId());
        baseMapper.deleteById(id);
        if (franchiseesRegistration != null) {
            String operatorType = "delete";
            if (followUpPlan != null && followUpPlan == 0) {
                operatorType = "删除跟进记录";
            } else if (followUpPlan != null && followUpPlan == 1) {
                operatorType = "删除跟进计划";
            }
            franchiseesRegistrationService.updateFllowUser(follow.getFranchiseesRegistrationId(), operatorType);
        }

    }

    @Override
    public IPage<FranchiseesRegistrationFollow> getRegistrationFollowByRegistrationId(Page<FranchiseesRegistrationFollow> page, Integer registrationId, Integer followUpPlan, Integer followUpPlanStatus) {
/*        LambdaQueryWrapper<FranchiseesRegistrationFollow> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FranchiseesRegistrationFollow::getFranchiseesRegistrationId,registrationId);
        queryWrapper.orderByDesc(FranchiseesRegistrationFollow::getFollowTime);
        return baseMapper.selectPage(page,queryWrapper);*/
        return baseMapper.getRegistrationFollowByRegistrationId(page, registrationId, followUpPlan, followUpPlanStatus);
    }

    @Override
    public FranchiseesRegistrationFollow saveOrUpdateFollow(FranchiseesRegistrationFollow vo) {
        // 先查询 当前这个人 创建了几条 数据 ；

        return null;
    }

    @Override
    public List<SimpleDateVo> getNextFranchiseesRegistrationTime(List<Integer> registrationId, Integer groupId) {
        String registrationIds = CommonUtil.IntegerToStringSql(registrationId);
        return baseMapper.getNextFranchiseesRegistrationTime(registrationIds, groupId);
    }

    @Override
    public List<Integer> getRegistrationIdByTime(Integer groupId, List<Integer> registrationIds, String startFlowDate, String endFlowDate) {

        List<Integer> registrationIdList = new ArrayList<>();

        LambdaQueryWrapper<FranchiseesRegistrationFollow> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FranchiseesRegistrationFollow::getGroupId, groupId);
        if (CollectionUtil.isNotEmpty(registrationIds)) {
            queryWrapper.in(FranchiseesRegistrationFollow::getFranchiseesRegistrationId, registrationIds);
        }
        if (StringUtils.isNotEmpty(startFlowDate)) {
            queryWrapper.gt(FranchiseesRegistrationFollow::getFollowTime, startFlowDate);
        }
        if (StringUtils.isNotEmpty(endFlowDate)) {
            queryWrapper.lt(FranchiseesRegistrationFollow::getFollowTime, endFlowDate);
        }
        queryWrapper.orderByDesc(FranchiseesRegistrationFollow::getFollowTime);

        List<FranchiseesRegistrationFollow> franchiseesRegistrationFollows = baseMapper.selectList(queryWrapper);
        if (CollectionUtil.isNotEmpty(franchiseesRegistrationFollows)) {
            registrationIdList = franchiseesRegistrationFollows.stream().map(FranchiseesRegistrationFollow::getFranchiseesRegistrationId).distinct().collect(Collectors.toList());
        }

        return registrationIdList;
    }

    @Override
    public Integer getFollowNumByRegistrationId(Integer registrationId) {
        LambdaQueryWrapper<FranchiseesRegistrationFollow> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FranchiseesRegistrationFollow::getFranchiseesRegistrationId, registrationId);
        queryWrapper.eq(FranchiseesRegistrationFollow::getIsDel, 0);
        return baseMapper.selectCount(queryWrapper);

    }

    @Override
    public FranchiseesRegistrationFollowVo saveFollowvo(FranchiseesRegistrationFollowVo vo) throws ParseException, IOException {


        FranchiseesRegistration franchiseesRegistration = franchiseesRegistrationService.findFranchiseesRegistrationById(vo.getFranchiseesRegistrationId());
        if (franchiseesRegistration != null) {
            //一个跟进计划不能同时生成多条已完成的跟进记录    跟进计划  是未完成状态  跟进记录已完成状态下
        /*    if(vo.getId() !=null){
                FranchiseesRegistrationFollow oldVo = getFollowById(vo.getId());
                if(oldVo !=null  && oldVo.getFollowUpPlanStatus().intValue() == 1 && vo.getFollowUpPlan().intValue() == 0 && vo.getOldFollowUpPlan() !=null && vo.getOldFollowUpPlan().intValue() ==1){
                    throw new IOException( vo.getId() + "一个跟进计划不能同时生成多条已完成的跟进记录！");
                }
            }*/
            FranchiseesRegistrationFollow follow = new FranchiseesRegistrationFollow();
            BeanUtils.copyProperties(vo, follow);

            if (follow.getFollowUpPlanStatus() != null && follow.getFollowUpPlanStatus().intValue() == FranchiseesRegistrationFollow.FOLLOW_UP_STATUS_CANCEL) {
                // 已取消的跟进计划 不产生新的数据
                saveOrUpdateVo(follow);
                // franchiseesRegistrationService.updateFllowUser(franchiseesRegistration.getId());
            } else {
                follow.setNextFollowTime(StringUtils.isNotEmpty(vo.getNextFollowTime()) ? DateUtil.parse(vo.getNextFollowTime()) : null);
                follow.setFollowTime(StringUtils.isNotEmpty(vo.getFollowTime()) ? DateUtil.parse(vo.getFollowTime()) : null);
                if (vo.getFollowUpPlan() != null && vo.getFollowUpPlan().intValue() == 1 && StringUtils.isNotEmpty(vo.getNextFollowTime())) {
                    if (follow.getNextFollowTime().before(new Date())) {
                        follow.setFollowUpPlanStatus(2);
                    } else {
                        follow.setFollowUpPlanStatus(0);
                    }
                    follow.setFollowTime(follow.getNextFollowTime());
                    follow.setFollowContent(follow.getFollowUpPlanContent());
                }
                if (vo.getFollowUpPlan() != null && vo.getFollowUpPlan().intValue() == 0) {
                    follow.setFollowUpPlanStatus(1);
                }
                saveOrUpdateVo(follow);
                if (vo.getFollowUpPlan() != null && vo.getFollowUpPlan().intValue() == 0) {
                    if (StringUtil.isNotBlank(vo.getNextFollowTime())) {
                        // 跟进记录 自动生成 跟进计划
                        follow.setId(null);
                        follow.setFollowUpPlan(1);
                        if (follow.getNextFollowTime().before(new Date())) {
                            follow.setFollowUpPlanStatus(2);
                        } else {
                            follow.setFollowUpPlanStatus(0);
                        }
                        follow.setFollowTime(follow.getNextFollowTime());
                        follow.setNextFollowTime(follow.getNextFollowTime());
                        follow.setFollowContent(follow.getFollowUpPlanContent());
                        saveOrUpdateVo(follow);
                    }
                    franchiseesRegistrationService.updateFllowUser(franchiseesRegistration.getId(), "自动生成跟进计划");

                    if (vo.getFollowUser() != null && franchiseesRegistration.getFollowUser() == null) {
                        franchiseesRegistration.setFollowUser(vo.getFollowUser());
                        franchiseesRegistration.setDistributionTime(new Date());
                        //  franchiseesRegistrationService.saveOrUpdateBean(franchiseesRegistration);
                    }

                    // 取 所有跟进记录的 最新 跟进日期

                    LambdaQueryWrapper<FranchiseesRegistrationFollow> followLambdaQueryWrapper = new LambdaQueryWrapper<>();
                    followLambdaQueryWrapper.eq(FranchiseesRegistrationFollow::getFranchiseesRegistrationId, franchiseesRegistration.getId());
                    followLambdaQueryWrapper.eq(FranchiseesRegistrationFollow::getIsDel, 0);
                    followLambdaQueryWrapper.eq(FranchiseesRegistrationFollow::getFollowUpPlan, 0);
                    followLambdaQueryWrapper.orderByDesc(FranchiseesRegistrationFollow::getFollowTime);
                    List<FranchiseesRegistrationFollow> followList = baseMapper.selectList(followLambdaQueryWrapper);
                    if (CollectionUtil.isNotEmpty(followList)) {
                        FranchiseesRegistrationFollow finalFollow = followList.get(0);
                        franchiseesRegistration.setFinalFollowTime(finalFollow.getFollowTime());
                        franchiseesRegistration.setNextFollowTime(finalFollow.getNextFollowTime());
                    }

               /*  franchiseesRegistration.setFinalFollowTime(follow.getFollowTime());
                franchiseesRegistration.setNextFollowTime(follow.getNextFollowTime());*/
                    franchiseesRegistration.setFollowType(FranchiseesRegistration.FLOW_TYPE_ING);
                    //  franchiseesRegistration.setFollowUser(vo.getCreateId());
                    franchiseesRegistrationService.saveOrUpdateBean(franchiseesRegistration);


                }
            }


        } else {
            throw new IOException(vo.getFranchiseesRegistrationId() + "线索不存在！");

        }

        return vo;
    }

    @Override
    public List<FranchiseesRegistrationFollow> getRegistrationFollows(Integer registrationId, Integer followUpPlan, Integer followUpPlanStatus) {
        return baseMapper.getRegistrationFollows(registrationId, followUpPlan, followUpPlanStatus);
    }

    @Override
    public void synChangeRegistrationFollowStatus(Integer groupId) {
        LambdaQueryWrapper<FranchiseesRegistrationFollow> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FranchiseesRegistrationFollow::getGroupId, groupId);
        queryWrapper.eq(FranchiseesRegistrationFollow::getIsDel, 0);
        queryWrapper.eq(FranchiseesRegistrationFollow::getFollowUpPlanStatus, FranchiseesRegistrationFollow.FOLLOW_UP_STATUS_NOT_STARTED);
        queryWrapper.eq(FranchiseesRegistrationFollow::getFollowUpPlan, 1);
        List<FranchiseesRegistrationFollow> franchiseesRegistrationFollows = baseMapper.selectList(queryWrapper);
        if (CollectionUtil.isNotEmpty(franchiseesRegistrationFollows)) {
            for (FranchiseesRegistrationFollow follow : franchiseesRegistrationFollows) {
                Date nextFollowTime = follow.getNextFollowTime();
                if (nextFollowTime != null && nextFollowTime.before(new Date())) {
                    follow.setFollowUpPlanStatus(FranchiseesRegistrationFollow.FOLLOW_UP_STATUS_OVERDUE);
                    baseMapper.updateById(follow);
                }

            }

        }
    }

    @Override
    public List<Integer> findAllGroupIds() {
        List<Integer> groupIds = new ArrayList<>();
        LambdaQueryWrapper<FranchiseesRegistrationFollow> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FranchiseesRegistrationFollow::getIsDel, 0);
        queryWrapper.eq(FranchiseesRegistrationFollow::getFollowUpPlanStatus, FranchiseesRegistrationFollow.FOLLOW_UP_STATUS_NOT_STARTED);
        queryWrapper.eq(FranchiseesRegistrationFollow::getFollowUpPlan, 1);
        queryWrapper.select(FranchiseesRegistrationFollow::getGroupId);
        List<FranchiseesRegistrationFollow> franchiseesRegistrationFollows = baseMapper.selectList(queryWrapper);
        if (CollectionUtil.isNotEmpty(franchiseesRegistrationFollows)) {
            groupIds = franchiseesRegistrationFollows.stream().map(FranchiseesRegistrationFollow::getGroupId).distinct().collect(Collectors.toList());
            groupIds = groupIds.stream().distinct().collect(Collectors.toList());
        }
        return groupIds;
    }

    @Override
    public List<FranchiseesRegistrationFollow> getFollowByRegistrationId(Integer registrationId, Integer followUpPlan, Boolean filterClose) {
        LambdaQueryWrapper<FranchiseesRegistrationFollow> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FranchiseesRegistrationFollow::getIsDel, 0);
        queryWrapper.eq(FranchiseesRegistrationFollow::getFollowUpPlan, followUpPlan);
        queryWrapper.eq(FranchiseesRegistrationFollow::getFranchiseesRegistrationId, registrationId);
        if (filterClose) {
            queryWrapper.ne(FranchiseesRegistrationFollow::getFollowUpPlanStatus, FranchiseesRegistrationFollow.FOLLOW_UP_STATUS_CANCEL);
        }
        queryWrapper.orderByDesc(FranchiseesRegistrationFollow::getFollowTime);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public void updateFollowUpPlanStatusByRegistrationId(Integer groupId, List<Integer> registrationIds, Integer userId, Integer followUpStatusCancel, String followUpPlanReason) {
        if (CollectionUtil.isNotEmpty(registrationIds)) {
            baseMapper.updateFollowUpPlanStatusByRegistrationId(groupId, CommonUtil.IntegerToStringSql(registrationIds), userId, followUpStatusCancel, followUpPlanReason);
        }
    }

    @Override
    public Map<Integer, FranchiseesRegistrationFollow> getFinalFollowUserByRegistrationId(Integer groupId, List<Integer> ids) {
        if (CollectionUtil.isNotEmpty(ids)) {
            LambdaQueryWrapper<FranchiseesRegistrationFollow> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(FranchiseesRegistrationFollow::getIsDel, 0);
            queryWrapper.ne(FranchiseesRegistrationFollow::getFollowUpPlanStatus, FranchiseesRegistrationFollow.FOLLOW_UP_STATUS_CANCEL);
            queryWrapper.eq(FranchiseesRegistrationFollow::getFollowUpPlan, 0);
            queryWrapper.in(FranchiseesRegistrationFollow::getFranchiseesRegistrationId, ids);
            queryWrapper.orderByDesc(FranchiseesRegistrationFollow::getFranchiseesRegistrationId);
            queryWrapper.orderByDesc(FranchiseesRegistrationFollow::getFollowTime);
            List<FranchiseesRegistrationFollow> franchiseesRegistrationFollows = baseMapper.selectList(queryWrapper);
            if (CollectionUtil.isNotEmpty(franchiseesRegistrationFollows)) {
                Map<Integer, FranchiseesRegistrationFollow> map = franchiseesRegistrationFollows.stream().collect(Collectors.toMap(FranchiseesRegistrationFollow::getFranchiseesRegistrationId, Function.identity(), (v1, v2) -> v1));
                return map;
            }
        }
        return new HashMap<>();
    }

    @Override
    public List<FranchiseesRegistrationFollow> getRegistrationFollowsByPlanId(Integer franchiseesRegistrationId, Integer planId) {
        LambdaQueryWrapper<FranchiseesRegistrationFollow> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FranchiseesRegistrationFollow::getIsDel, 0);
        queryWrapper.eq(FranchiseesRegistrationFollow::getFollowUpPlanId, planId);
        queryWrapper.eq(FranchiseesRegistrationFollow::getFranchiseesRegistrationId, franchiseesRegistrationId);
        queryWrapper.orderByDesc(FranchiseesRegistrationFollow::getFollowTime);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public FranchiseesRegistrationFollowVo saveNewFollowvo(FranchiseesRegistrationFollowVo vo) throws ParseException {

        FranchiseesRegistration franchiseesRegistration = franchiseesRegistrationService.findFranchiseesRegistrationById(vo.getFranchiseesRegistrationId());
        if (franchiseesRegistration != null) {
            //一个跟进计划不能同时生成多条已完成的跟进记录    跟进计划  是未完成状态  跟进记录已完成状态下
            if (vo.getFollowUpPlanStatus() !=null && vo.getFollowUpPlanStatus().intValue() == FranchiseesRegistrationFollow.FOLLOW_UP_STATUS_CANCEL ) {
                // 已取消的跟进计划 不产生新的数据
                franchiseesRegistrationPlanService.saveOrUpdateVo(vo);
            } else {
                FranchiseesRegistrationFollow follow = new FranchiseesRegistrationFollow();
                BeanUtils.copyProperties(vo, follow);
                follow.setNextFollowTime(StringUtils.isNotEmpty(vo.getNextFollowTime()) ? DateUtil.parse(vo.getNextFollowTime()) : null);
                follow.setFollowTime(StringUtils.isNotEmpty(vo.getFollowTime()) ? DateUtil.parse(vo.getFollowTime()) : null);
                if (vo.getFollowUpPlan() != null && vo.getFollowUpPlan().intValue() == 1 && StringUtils.isNotEmpty(vo.getNextFollowTime())) {
                    if (follow.getNextFollowTime().before(new Date())) {
                        follow.setFollowUpPlanStatus(2);
                    } else {
                        follow.setFollowUpPlanStatus(0);
                    }
                    follow.setFollowTime(follow.getNextFollowTime());
                    follow.setFollowContent(follow.getFollowUpPlanContent());
                }
                if (vo.getFollowUpPlan() != null && vo.getFollowUpPlan().intValue() == 0) {
                    follow.setFollowUpPlanStatus(1);
                }

                Integer palnId = null;
                Integer planType = vo.getFollowUpPlan();
                Boolean isChange = false;
                if (vo.getOldFollowUpPlan() != null && vo.getOldFollowUpPlan().intValue() == FranchiseesRegistrationFollow.FOLLOW_UP_PLAN) {
                    // 如果是 跟进计划 转 跟进记录 ，生成跟进记录 删除跟进计划
                    if (planType != vo.getOldFollowUpPlan().intValue()) {
                        isChange = true;
                    }

                    planType = FranchiseesRegistrationFollow.FOLLOW_UP_PLAN;
                }

                if (planType == FranchiseesRegistrationFollow.FOLLOW_UP_PLAN) {
                    // 跟进计划
                    FranchiseesRegistrationPlan plan = new FranchiseesRegistrationPlan();
                    BeanUtils.copyProperties(follow, plan);
                    if (isChange) {
                        plan.setIsDel(1);
                    }
                    plan = franchiseesRegistrationPlanService.saveOrUpdateBean(plan);
                    palnId = plan.getId();
                } else {
                    // 如果是 跟进计划 转 跟进记录 ，生成跟进记录 删除跟进计划
                    saveOrUpdateVo(follow);
                }

                if (isChange) {
                    follow.setId(null);
                    follow.setFollowUpPlanId(palnId);
                    follow.setFollowUpPlan(FranchiseesRegistrationFollow.FOLLOW_UP_FOLLOW);
                    follow = saveOrUpdateVo(follow);
                }


                if (vo.getFollowUpPlan() != null && vo.getFollowUpPlan().intValue() == 0) {
                    if (StringUtil.isNotBlank(vo.getNextFollowTime())) {
                        // 跟进记录 自动生成 跟进计划
                        follow.setId(null);
                        follow.setFollowUpPlan(1);
                        if (follow.getNextFollowTime().before(new Date())) {
                            follow.setFollowUpPlanStatus(2);
                        } else {
                            follow.setFollowUpPlanStatus(0);
                        }
                        follow.setFollowTime(follow.getNextFollowTime());
                        follow.setNextFollowTime(follow.getNextFollowTime());
                        follow.setFollowContent(follow.getFollowUpPlanContent());
                        follow.setFollowUpPlanId(palnId);
                        FranchiseesRegistrationPlan plan = new FranchiseesRegistrationPlan();
                        BeanUtils.copyProperties(follow, plan);
                        plan = franchiseesRegistrationPlanService.saveOrUpdateBean(plan);
                       // saveOrUpdateVo(follow);
                     /*   palnId = plan.getId();
                        follow.setFollowUpPlanId(palnId);
                        saveOrUpdateVo(follow);*/
                    }
                    franchiseesRegistrationService.updateFllowUser(franchiseesRegistration.getId(), "自动生成跟进计划");

                    if (vo.getFollowUser() != null && franchiseesRegistration.getFollowUser() == null) {
                        franchiseesRegistration.setFollowUser(vo.getFollowUser());
                        franchiseesRegistration.setDistributionTime(new Date());
                        //  franchiseesRegistrationService.saveOrUpdateBean(franchiseesRegistration);
                    }

                    // 取 所有跟进记录的 最新 跟进日期

                    LambdaQueryWrapper<FranchiseesRegistrationFollow> followLambdaQueryWrapper = new LambdaQueryWrapper<>();
                    followLambdaQueryWrapper.eq(FranchiseesRegistrationFollow::getFranchiseesRegistrationId, franchiseesRegistration.getId());
                    followLambdaQueryWrapper.eq(FranchiseesRegistrationFollow::getIsDel, 0);
                    followLambdaQueryWrapper.eq(FranchiseesRegistrationFollow::getFollowUpPlan, 0);
                    followLambdaQueryWrapper.orderByDesc(FranchiseesRegistrationFollow::getFollowTime);
                    List<FranchiseesRegistrationFollow> followList = baseMapper.selectList(followLambdaQueryWrapper);
                    if (CollectionUtil.isNotEmpty(followList)) {
                        FranchiseesRegistrationFollow finalFollow = followList.get(0);
                        franchiseesRegistration.setFinalFollowTime(finalFollow.getFollowTime());
                        franchiseesRegistration.setNextFollowTime(finalFollow.getNextFollowTime());
                    }
                    franchiseesRegistrationService.saveOrUpdateBean(franchiseesRegistration);
               /*  franchiseesRegistration.setFinalFollowTime(follow.getFollowTime());
                franchiseesRegistration.setNextFollowTime(follow.getNextFollowTime());*/
                    franchiseesRegistration = franchiseesRegistrationService.findFranchiseesRegistrationById(franchiseesRegistration.getId());
                    if(franchiseesRegistration.getFollowUser() !=null && franchiseesRegistration.getFollowUser().intValue() == follow.getCreateId().intValue()){
                        // 如果当前 跟进记录是 线索的跟进人 创建的 则修改状态
                        franchiseesRegistration.setFollowType(FranchiseesRegistration.FLOW_TYPE_ING);
                    }
                    //  franchiseesRegistration.setFollowUser(vo.getCreateId());
                    franchiseesRegistrationService.saveOrUpdateBean(franchiseesRegistration);


                }
            }

        }

        return vo;
    }

    @Override
    public Boolean deleteNewFollowById(Integer id, Integer groupId) {
        // 删除 跟进记录 如果 当前跟进记录是 最新的一个， 需要把 线索的 跟进人 换成 后面一个；
        FranchiseesRegistrationFollow follow = getFollowById(id);
        if (groupId != null && follow != null && follow.getGroupId().intValue() != groupId) {
            return false;
        }
        FranchiseesRegistration franchiseesRegistration = franchiseesRegistrationService.findFranchiseesRegistrationById(follow.getFranchiseesRegistrationId());
        baseMapper.deleteById(id);
        if (franchiseesRegistration != null) {
            String operatorType = "删除跟进记录";
            franchiseesRegistrationService.updateFllowUser(follow.getFranchiseesRegistrationId(), operatorType);
        }
        return true;
    }


}
